﻿/*
	Copyright (c) 2009, M.P. Bartnig & A. Gosman
	
	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:
	
	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.
	
	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE.
*/
package nl.sanity.utils
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	/**
	 * DisplayUtils
	 * Utilities for Display objects
	 * 
	 * @langversion ActionScript 3
	 * 
	 * @author M.P. Bartnig
	 * @since  16/03/2009
	 */
    public class DisplayUtils
	{
		public static function resizeBitmap( originalBitmap:Bitmap, maxW:Number, maxH:Number, crop:Boolean = true ):Bitmap
		{
			var resizedBitmap:Bitmap	= new Bitmap( originalBitmap.bitmapData.clone(), "auto", true );
			var scaleFactor:Number		= resizedBitmap.height / resizedBitmap.width;
			
			resizedBitmap.width 		= maxW;
			resizedBitmap.height 		= Math.round( resizedBitmap.width * scaleFactor );
			if ( resizedBitmap.height < maxH )
			{
				resizedBitmap.height = maxH;
				resizedBitmap.scaleX = resizedBitmap.scaleY;
			}
			
			scaleFactor = resizedBitmap.scaleX;
			
			var resizedBitmapData:BitmapData	= resizedBitmap.bitmapData;	
			var newWidth:Number 				= resizedBitmap.width;
			var newHeight:Number 				= resizedBitmap.height;
			
			var scaledBitmapData:BitmapData 	= new BitmapData( newWidth, newHeight, true, 0xFFFFFFFF );
			var scaleMatrix:Matrix				= new Matrix();
			
			scaleMatrix.scale( scaleFactor, scaleFactor );
			scaledBitmapData.draw( resizedBitmapData, scaleMatrix, null, null, null, true );
			
			resizedBitmap.bitmapData.dispose();
			resizedBitmap = new Bitmap( scaledBitmapData );
			
			if ( crop )
			{
				var cropRect:Rectangle				= new Rectangle(	( resizedBitmap.width - maxW ) * 0.5,
																		( resizedBitmap.height - maxH ) * 0.5,
																		maxW,
																		maxH );
				var croppedBitmapData:BitmapData	= cropBitmap( resizedBitmap, cropRect, true );
				var croppedBitmap:Bitmap			= new Bitmap( croppedBitmapData );
				resizedBitmap.bitmapData.dispose();
				
				return croppedBitmap;
			}
			
			return resizedBitmap;
		}
		
		public static function cropBitmap( originalBitmap:Bitmap, rect:Rectangle, smoothing:Boolean ):BitmapData
		{
			var bmpData1:BitmapData = new BitmapData( originalBitmap.width, originalBitmap.height, true, 0x00000000 );
			var bmpData2:BitmapData = new BitmapData( rect.width, rect.height, true, 0x00000000 );
		 
			bmpData1.draw( originalBitmap, null, null, null, null, smoothing );
			bmpData2.copyPixels( bmpData1, rect, new Point( 0, 0 ) );
		 
			bmpData1.dispose();
		 
			return bmpData2;
		}
		
		public static function getBitmapSlice( bitmap:Bitmap, sourceRect:Rectangle ):Bitmap
		{
			var destPoint:Point		= new Point( 0, 0 );
			var bitmapSlice:Bitmap	= new Bitmap( new BitmapData( sourceRect.width, sourceRect.height, true, 0x00000000 ) );
			
			bitmapSlice.bitmapData.copyPixels( bitmap.bitmapData, sourceRect, destPoint );
	
			return bitmapSlice;
		}
		
		/**
		 * Returns the distance from the registration point of the specified 
		 * object to the bottom-most visible pixel, ignoring any region
		 * that is not visible due to masking. For example, if a display
		 * object contains a 100-pixel-high shape and a 50-pixel-high mask,
		 * getVisibleHeight() will return 50, whereas DisplayObject's
		 * "height" variable would yield 100. 
		 * 
		 * The maximum measureable dimensions of the supplied object is 2000x2000.
		 * 
		 * @see	http://www.moock.org/blog/archives/000292.html
		 */
		public static function getVisibleHeight ( object:DisplayObject ):Number 
		{
			var bounds:Rectangle	= getColorBoundsRect( object );
			return bounds.y + bounds.height;
		}
		
		/**
		 * Returns the distance from the registration point of the specified 
		 * object to the right-most visible pixel, ignoring any region
		 * that is not visible due to masking. For example, if a display
		 * object contains a 100-pixel-wide shape and a 50-pixel-wide mask,
		 * getVisibleWidth() will return 50, whereas DisplayObject's
		 * "width" variable would yield 100. 
		 * 
		 * The maximum measureable dimensions of the supplied object is 2000x2000.
		 * 
		 * @see	http://www.moock.org/blog/archives/000292.html
		 */
		public static function getVisibleWidth ( object :DisplayObject):Number 
		{
			var bounds:Rectangle	= getColorBoundsRect( object );
		  	return bounds.x + bounds.width;
		}
		
		public static function getVisibleDimension( object:DisplayObject ):Point
		{
			return new Point( DisplayUtils.getVisibleWidth( object ), DisplayUtils.getVisibleHeight( object ) );
		}
		
		public static function getColorBoundsRect( object:DisplayObject ):Rectangle
		{
			var bitmapDataSize:int = 2000;
			
			var bitmapData:BitmapData 	= new BitmapData(bitmapDataSize, bitmapDataSize, true, 0 );
			bitmapData.draw( object);
			
			var bounds:Rectangle		= bitmapData.getColorBoundsRect( 0xFF000000, 0x00000000, false );
		  	bitmapData.dispose();
		  	
		  	return bounds;
		}

    }

}